local DataModel = import(".DataModel")
local Badge = class("Badge",DataModel)

local DefaultList={}

function Badge:ctor()
	Badge.super.ctor(self)
	self:clear()

	DefaultList[Constant.FUNC_TYPE_ACHIEVEMENT] = 0
	DefaultList[Constant.FUNC_TYPE_TASK] = 0
	DefaultList[Constant.FUNC_TYPE_CORE_PROP] = 0
	DefaultList[Constant.FUNC_TYPE_MSDK_COMMUNITY] = 0
	DefaultList[Constant.FUNC_TYPE_MAIL] = 0

end

function Badge:clear()
	self.nums={}
end

function Badge:getDefaultNum(id)
	local default = DefaultList[id]
	if default then
		return default
	else
		return 0
	end
end

function Badge:read(value)
	for _,badge in pairs(value) do
		local id = badge.id
		local num = badge.num or 0
		self.nums[id] = num
	end

	self.nums[Constant.FUNC_TYPE_SHOP] = 0

	--dump(self.nums,"Badge:read")
end

function Badge:write(value)
	for _id,_num in pairs(self.nums) do
		table.insert(value,{id=_id,num=_num})
	end
	--dump(self.nums,"Badge:write")
end

-- id funcId
function Badge:getNum(id)
	local num = self.nums[id]
	num = (num or self:getDefaultNum(id))
	return num
end

-- id funcId
function Badge:setNum(id,num)
	local old = self.nums[id]
	if old then
		if old~=num then
			self.nums[id] = num
			self:dispatchChange(id,num)
			return true
		end
	else
		local default = self:getDefaultNum(id)
		self.nums[id] = num
		if default ~= num then
			self:dispatchChange(id,num)
			return true
		end
	end
	return false
end

function Badge:increase(id)
	local old = self.nums[id] or 0
	local num = old + 1
	self:setNum(id,num)
end

function Badge:reduce(id)
	local old = self.nums[id] or 0
	local num = math.max(old - 1, 0)
	self:setNum(id,num)
end

return Badge